﻿using System;
using System.Collections.Generic;
using System.Linq;
using HtmlInputExtractor.Common;

namespace HtmlInputExtractor {
	internal class Program {
		private static void Main(string[] args) {
			if (args.Length < 1) {
				Console.WriteLine("usage: HtmlInputExtractor <input> [<old_output>]");
				Console.WriteLine("Please use redirect(>) to output to files");
				Console.WriteLine();
				Console.WriteLine("input: html file to extract");
				Console.WriteLine("old_output: existing output file(csv)");
				return;
			}

			var oldInputs = args.Length >= 2
			                		? Common.HtmlInputExtractor.ExtractFromCsv(args[1])
			                		: new List<HtmlInput>();
			var inputs = Common.HtmlInputExtractor.Extract(args[0], oldInputs);

			var index = 1;

			Console.WriteLine(
					"No.,名前,種別,初期値,Tab順,数字,英字,記号,カナ,全角,日付,その他,必須項目,最小文字数,最大文字数,最小値,最大値,文字寄せ,フォーマット,遷移先,遷移方法,入力,出力,DB入力,DB出力,DB項目名,備考,ID");
			foreach (var input in inputs) {
				Console.Write(GetString(index) + ",");
				Console.Write(GetString(input.Name) + ",");
				Console.Write(GetString(input.Type) + ",");
				Console.Write(GetString(input.Value) + ",");
				Console.Write(GetString(input.TabOrder) + ",");
				Console.Write(GetString(input.IsNumber) + ",");
				Console.Write(GetString(input.IsAlphabet) + ",");
				Console.Write(GetString(input.IsSign) + ",");
				Console.Write(GetString(input.IsKana) + ",");
				Console.Write(GetString(input.IsZenkaku) + ",");
				Console.Write(GetString(input.IsDate) + ",");
				Console.Write(GetString(input.IsOther) + ",");
				Console.Write(GetString(input.IsRequired) + ",");
				Console.Write(GetString(input.MinLength) + ",");
				Console.Write(GetString(input.MaxLength) + ",");
				Console.Write(GetString(input.MinValue) + ",");
				Console.Write(GetString(input.MaxValue) + ",");
				Console.Write(GetString(input.Alignment) + ",");
				Console.Write(GetString(input.Format) + ",");
				Console.Write(GetString(input.Transition) + ",");
				Console.Write(GetString(input.TransitionMethod) + ",");
				Console.Write(GetString(input.IsInput) + ",");
				Console.Write(GetString(input.IsOutput) + ",");
				Console.Write(GetString(input.IsDbInput) + ",");
				Console.Write(GetString(input.IsDbOutput) + ",");
				Console.Write(GetString(input.DbName) + ",");
				Console.Write(GetString(input.Others) + ",");
				Console.WriteLine(GetString(input.Id));
				index++;
			}
		}

		private static string GetString(int? value) {
			return value + "";
		}

		private static string GetString(string str) {
			return Escape(str ?? "");
		}

		private static string GetString(ComponentType type) {
			return GetTypeString(type);
		}

		private static string GetString(bool value) {
			return value ? "●" : "";
		}

		private static string GetTypeString(ComponentType type) {
			switch (type) {
			case ComponentType.Label:
				return "ラベル";
			case ComponentType.Link:
				return "リンク";
			case ComponentType.Text:
				return "テキストボックス";
			case ComponentType.Password:
				return "パスワード";
			case ComponentType.File:
				return "ファイル選択";
			case ComponentType.CheckBox:
				return "チェックボックス";
			case ComponentType.RadioButton:
				return "ラジオボタン";
			case ComponentType.Hidden:
				return "Hidden";
			case ComponentType.Submit:
				return "送信ボタン";
			case ComponentType.Reset:
				return "リセットボタン";
			case ComponentType.Button:
				return "ボタン";
			case ComponentType.ImageButton:
				return "イメージボタン";
			default:
				throw new ArgumentOutOfRangeException();
			}
		}

		private static readonly char[] EscapedChars = { ',', '"' };

		private static string Escape(string str) {
			if (!EscapedChars.Any(str.Contains))
				return str;
			return '"' + str.Replace("\"", "\"\"") + '"';
		}
	}
}